package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements ICompanyService {
    @Override
    public CompanyModel getCompanyModel() {
        /**
         * 数据来源
         * 1.如果第一个人访问应该访问数据库
         *  将数据存入reids
         * 2.其他人访问redis
         */
        final CompanyModel[] companyModel = {new CompanyModel()};

//        for (int i = 0; i < 1000; i++) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
        companyModel[0] = getData();
//                    System.out.println(Thread.currentThread().getName());
//                }
//            }, "线程名称" + (i + 1) + "---").start();

//        }
//        首先判断reids里面是否有数据，如果有，则查询reidis，如果没有，则查询数据库
        return companyModel[0];
    }

    private CompanyModel getData() {
        CompanyDao companyDao = new CompanyImpl();
        CompanyModel companyModel = new CompanyModel();
        Jedis jedis = new Jedis();
        if (jedis.exists("companyModel_id")) {
            companyModel.setId(Integer.parseInt(jedis.get("companyModel_id")));
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setAd(jedis.get("ad"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setLinePhone(jedis.get("line_phone"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setVersion(jedis.get("version"));
            companyModel.setLogo(jedis.get("companyModel_id"));
            System.out.println("redis");
        } else {
            companyModel = companyDao.getCompanyModel();//查询数据库

            jedis.set("companyModel_id", companyModel.getId() + "");
//            jedis.expire("companyModel_id",60*30);
            jedis.setex("companyModel_id", 60 * 30, companyModel.getId() + "");
            jedis.set("logo", companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("ad", companyModel.getAd());
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("line_phone", companyModel.getLinePhone());
            jedis.set("address", companyModel.getAddress());
            jedis.set("version", companyModel.getVersion());
            System.out.println("mysql");
        }
        return companyModel;
    }

    public int add(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int result = companyDao.add(companyModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("companyModel_id");
//            jedis.flushAll();删除所有redis数据
//            jedis.flushDB();删除当前数据库
        }
        return result;
    }

    public int update(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int result = companyDao.update(companyModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("companyModel_id");
        }
        return result;
    }

    public int del(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int result = companyDao.del(companyModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("companyModel_id");
        }
        return result;
    }
}
